﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace xmltest
{


    class cunchu
    {
        List<BasePower> basepower = new List<BasePower>();
        List<BaseVoltage> basevoltage = new List<BaseVoltage>();
        List<Connector> connector= new List<Connector>();
        List<CompositeSwitch> compositeswitch= new List<CompositeSwitch>();
        List<Bay> bay = new List<Bay>();
        List<Disconnector> disconnector = new List<Disconnector>();
        List<Fuse> fuse = new List<Fuse>();
        List<GroundDisconnector> grounddisconnector = new List<GroundDisconnector>();
        List<LoadBreakSwitch> loadbreakswitch = new List<LoadBreakSwitch>();
        List<PowerTransformer> powertransformer = new List<PowerTransformer>();
        List<TransformerWinding> transformerwinding = new List<TransformerWinding>();
        List<BusbarSection> busbarsection = new List<BusbarSection>();
        List<Substation> substation = new List<Substation>();
        List<Breaker> breaker = new List<Breaker>();
        List<Circuit> circuit = new List<Circuit>();
        List<ACLineSegment> aclinesegment = new List<ACLineSegment>();
        List<Pole> pole = new List<Pole>();
        List<ConnectivityNode> connectivitynode = new List<ConnectivityNode>();
        List<Terminal> terminal = new List<Terminal>();
        public void Exec()
        {
            XmlReader reader = XmlReader.Create(@"e://test.xml");
            while (!reader.EOF)
            {   
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name.Equals("cim:BasePower"))
                    {
                        BasePower c = new BasePower();
                        reader.MoveToAttribute("ID");
                        c.id = reader.ReadContentAsString();
                        reader.Read(); reader.Read(); reader.Read();
                        c.power = reader.ReadContentAsString();
                        basepower.Add(c);
                    }
                    else if (reader.Name.Equals("cim:BaseVoltage"))
                    {
                        BaseVoltage c = new BaseVoltage();
                        reader.MoveToAttribute("ID");
                        c.id = reader.ReadContentAsString();
                        reader.Read(); reader.Read(); reader.Read();
                        c.voltage = reader.ReadContentAsString();
                        basevoltage.Add(c);
                    }
                    else if (reader.Name.Equals("cim:CompositeSwitch"))
                    {
                        compositeswitch.Add(readCompositeSwitch(reader));
                    }
                    else if (reader.Name.Equals("cim:Connector"))
                    {
                        connector.Add(readConnector(reader));
                    }
                    else if (reader.Name.Equals("cim:Bay"))
                    {
                        bay.Add(readBay(reader));
                    }
                    else if (reader.Name.Equals("cim:Disconnector"))
                    {
                        disconnector.Add(readDisconnector(reader));
                    }
                    else if (reader.Name.Equals("cim:Fuse"))
                    {
                        fuse.Add(readFuse(reader));
                    }
                    else if (reader.Name.Equals("cim:GroundDisconnector"))
                    {
                        grounddisconnector.Add(readGroundDisconnector(reader));
                    }
                    else if (reader.Name.Equals("cim:LoadBreakSwitch"))
                    {
                        loadbreakswitch.Add(readLoadBreakSwitch(reader));
                    }
                    else if (reader.Name.Equals("cim:PowerTransformer"))
                    {
                        powertransformer.Add(readPowerTransformer(reader));
                    }
                    else if (reader.Name.Equals("cim:TransformerWinding"))
                    {
                        transformerwinding.Add(readTransformerWinding(reader));
                    }
                    else if (reader.Name.Equals("cim:BusbarSection"))
                    {
                        busbarsection.Add(readBusbarSection(reader));

                    }
                    else if (reader.Name.Equals("cim:Substation"))
                    {
                        substation.Add(readSubstation(reader));
                    }
                    else if (reader.Name.Equals("cim:Breaker"))
                    {
                        breaker.Add(readBreaker(reader));
                    }
                    else if (reader.Name.Equals("cim:Circuit"))
                    {
                        circuit.Add(readCircuit(reader));
                    }
                    else if (reader.Name.Equals("cim:ACLineSegment"))
                    {
                        aclinesegment.Add(readACLineSegment(reader));
                    }
                    else if (reader.Name.Equals("cim:Pole"))
                    {
                        pole.Add(readPole(reader));
                    }
                    else if (reader.Name.Equals("cim:ConnectivityNode"))
                    {
                        ConnectivityNode c = new ConnectivityNode();
                        reader.MoveToAttribute("ID");
                        c.id = reader.ReadContentAsString();
                        connectivitynode.Add(c);

                    }
                    else if (reader.Name.Equals("cim:Terminal"))
                    {
                        terminal.Add(readTerminal(reader));

                    }
                                        
                    }


            } 
            reader.Close();
            }
       
         

        
        /*reader 到达cim:CompositeSwitch根部的XmlReader流。
         返回在reader当前位置的完整的CompositeSwitch对象。*/
        private CompositeSwitch readCompositeSwitch(XmlReader reader)
        {
            CompositeSwitch c = new CompositeSwitch();
            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:CompositeSwitch")))
            {
                if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.name = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Model") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.model = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
           }
        private Connector readConnector(XmlReader reader)
        {
            Connector c = new Connector();

            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Connector")))
            {
                
                 if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }
               
                else reader.Read();
            }
            return c;
        }
        private Disconnector readDisconnector(XmlReader reader)
        {
            Disconnector c = new Disconnector();

            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Disconnector")))
            {

                if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Switch.normalOpen") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.normalopen = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private Fuse readFuse(XmlReader reader)
        {
            Fuse c = new Fuse();

            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Fuse")))
            {
                if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.name = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Model") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.model = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Switch.normalOpen") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.normalopen = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private GroundDisconnector readGroundDisconnector(XmlReader reader)
        {
            GroundDisconnector c = new GroundDisconnector();

            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:GroundDisconnector")))
            {

                if (reader.Name.Equals("cim:ScadaName") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.scadaname= reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Switch.normalOpen") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.normalopen = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
            private Bay readBay(XmlReader reader) 
               {
                        Bay c = new Bay();
                        reader.MoveToAttribute("ID");
                        c.id = reader.ReadContentAsString();
                       
                while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Bay")))
                {
                    if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element) 
                    { 
                        reader.Read();
                        c.name = reader.ReadContentAsString();
                    }
                   else reader.Read();
                } 
                 return c;
                }
            private LoadBreakSwitch readLoadBreakSwitch(XmlReader reader)
            {
                LoadBreakSwitch c = new LoadBreakSwitch();

                reader.MoveToAttribute("ID");
                c.id = reader.ReadContentAsString();
                while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:LoadBreakSwitch")))
                {

                    if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                    {
                        reader.Read();
                        c.name = reader.ReadContentAsString();
                    }
                    else if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                    {
                        reader.Read();
                        c.GIStype = reader.ReadContentAsString();
                    }
                    else if (reader.Name.Equals("cim:Model") && reader.NodeType == XmlNodeType.Element)
                    {
                        reader.Read();
                        c.model = reader.ReadContentAsString();
                    }
                    else if (reader.Name.Equals("cim:Switch.normalOpen") && reader.NodeType == XmlNodeType.Element)
                    {
                        reader.Read();
                        c.normalopen = reader.ReadContentAsString();
                    }
                    else if (reader.Name.Equals("cim:Connect_Type") && reader.NodeType == XmlNodeType.Element)
                    {
                        reader.Read();
                        c.connectype = reader.ReadContentAsString();
                    }                  
                    else reader.Read();
                }
                return c;
            }
            private PowerTransformer readPowerTransformer(XmlReader reader)
            {
                PowerTransformer c = new PowerTransformer();

                reader.MoveToAttribute("ID");
                c.id = reader.ReadContentAsString();
                while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:PowerTransformer")))
                {

                    if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                    {
                        reader.Read();
                        c.name = reader.ReadContentAsString();
                    }
                    else if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                    {
                        reader.Read();
                        c.GIStype = reader.ReadContentAsString();
                    }
                    else if (reader.Name.Equals("cim:Model") && reader.NodeType == XmlNodeType.Element)
                    {
                        reader.Read();
                        c.model = reader.ReadContentAsString();
                    }
                    else if (reader.Name.Equals("cim:ratedMVA") && reader.NodeType == XmlNodeType.Element)
                    {
                        reader.Read();
                        c.ratedMVA = reader.ReadContentAsString();
                    }
                    else if (reader.Name.Equals("cim:Serial_Number") && reader.NodeType == XmlNodeType.Element)
                    {
                        reader.Read();
                        c.SerialNumber = reader.ReadContentAsString();
                    }
                    else reader.Read();
                }
                return c;
            }           
           private TransformerWinding readTransformerWinding(XmlReader reader)
            {
                TransformerWinding c = new TransformerWinding();
                reader.MoveToAttribute("ID");
                c.id = reader.ReadContentAsString();
                while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:TransformerWinding")))
                {
                    if (reader.Name.Equals("cim:Naming.description") && reader.NodeType == XmlNodeType.Element)
                    {
                        reader.Read();
                        c.description = reader.ReadContentAsString();
                    }
                    else reader.Read();
                }
                return c;
            }
           private BusbarSection readBusbarSection(XmlReader reader)
           {
               BusbarSection c = new BusbarSection();

               reader.MoveToAttribute("ID");
               c.id = reader.ReadContentAsString();
               while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:BusbarSection")))
               {

                  if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                   {
                       reader.Read();
                       c.GIStype = reader.ReadContentAsString();
                   }
                  else if (reader.Name.Equals("cim:Normal_Voltage") && reader.NodeType == XmlNodeType.Element)
                   {
                       reader.Read();
                       c.normolvoltage = reader.ReadContentAsString();
                   }
                   else reader.Read();
               }
               return c;
           }
           private ACLineSegment readACLineSegment(XmlReader reader)
           {
               ACLineSegment c = new ACLineSegment();
               reader.MoveToAttribute("ID");
               c.id = reader.ReadContentAsString();
               while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:ACLineSegment")))
               {
                   if (reader.Name.Equals("cim:LongLength") && reader.NodeType == XmlNodeType.Element)
                   {
                       reader.Read();
                       c.length = reader.ReadContentAsString();
                   }
                   else if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                   {
                       reader.Read();
                       c.GIStype = reader.ReadContentAsString();
                   }
                   else if (reader.Name.Equals("cim:Model") && reader.NodeType == XmlNodeType.Element)
                   {
                       reader.Read();
                       c.model = reader.ReadContentAsString();
                   }
                   else reader.Read();
               }
               return c;
           }
           private Pole readPole(XmlReader reader)
           {
               Pole c = new Pole();
               reader.MoveToAttribute("ID");
               c.id = reader.ReadContentAsString();
               while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Pole")))
               {

                   if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                   {
                       reader.Read();
                       c.GIStype = reader.ReadContentAsString();
                   }

                   else reader.Read();
               }
               return c;
           }
           private Substation readSubstation(XmlReader reader)
           {
               Substation c = new Substation();
               reader.MoveToAttribute("ID");
               c.id = reader.ReadContentAsString();
               while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Substation")))
               {
                   if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                   {
                       reader.Read();
                       c.name = reader.ReadContentAsString();
                   }
                   else reader.Read();
               }
               return c;
           }
           private  Breaker readBreaker(XmlReader reader)
           {
                Breaker c = new Breaker();
               reader.MoveToAttribute("ID");
               c.id = reader.ReadContentAsString();
               while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Breaker")))
               {
                   if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                   {
                       reader.Read();
                       c.name = reader.ReadContentAsString();
                   }
                  else if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                   {
                       reader.Read();
                       c.GIStype = reader.ReadContentAsString();
                   }
                   else if (reader.Name.Equals("cim:Switch.normalOpen") && reader.NodeType == XmlNodeType.Element)
                   {
                       reader.Read();
                       c.normalopen = reader.ReadContentAsString();
                   }
                   else reader.Read();
               }
               return c;
           }     
           private Circuit readCircuit(XmlReader reader)
           {
                Circuit c = new Circuit();
               reader.MoveToAttribute("ID");
               c.id = reader.ReadContentAsString();
               while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Circuit")))
               {
                   if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                   {
                       reader.Read();
                       c.name = reader.ReadContentAsString();
                   }
                   else reader.Read();
               }
               return c;
           }
           private Terminal readTerminal(XmlReader reader)
           {
               Terminal c = new Terminal();
               reader.MoveToAttribute("ID");
               c.id = reader.ReadContentAsString();
               while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Terminal")))
               {
                   if (reader.Name.Equals("cim:Terminal.ConductingEquipment"))
                   {
                       reader.MoveToAttribute("resource");
                       c.ceres = reader.ReadContentAsString();
                       c.ceres = c.ceres.Substring(1);
                       if (c.ceres.Substring(c.ceres.Length - 2, 2).Equals("_1") || c.ceres.Substring(c.ceres.Length - 2, 2).Equals("_2"))
                       {
                           c.ceres = c.ceres.Substring(0, c.ceres.Length - 2);
                       }
                   }
                   else if (reader.Name.Equals("cim:Terminal.ConnectivityNode"))
                   {
                       reader.MoveToAttribute("resource");
                       c.cnres = reader.ReadContentAsString();
                       c.cnres = c.cnres.Substring(1);
                   }
                   else reader.Read();
               }
               return c;
           }                            
                        
        


        }
    
            



 }


   
    
